//
//  XMChatVoiceCell.m
//  XMJJ_zmy
//
//  Created by 丁浩 on 2025/7/5.
//

#import "XMChatVoiceCell.h"
#import <AVFoundation/AVFoundation.h>

@interface XMChatVoiceCell ()

//@property (nonatomic, strong) UIImageView *avatarImg;
@property (nonatomic, strong) UIButton *voiceBtn;

@property (nonatomic, strong) UIImageView *voiceIcon;
@property (nonatomic, strong) UILabel *longLabel;
@property (nonatomic, strong) UIImageView *msgVoiceIcon;
@property (nonatomic, strong) AVAudioPlayer *audioPlayer;

@property (nonatomic, copy) NSString *voiceStr;

@end

@implementation XMChatVoiceCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    self.backgroundColor = kColorHEX(0xF7F7F7);
    [self addSubview:self.avatarImg];
    [self addSubview:self.voiceBtn];
    [self.voiceBtn addSubview:self.longLabel];
    [self.voiceBtn addSubview:self.msgVoiceIcon];
}

- (void)setContentWithMessage:(V2TIMMessage *)message
                 senderAvatar:(NSString *)senderAvatarImg
               receiverAvatar:(NSString *)receiverAvatarImg {
    self.isSelf = message.isSelf;
    if (message.elemType == V2TIM_ELEM_TYPE_SOUND) {
        _voiceBtn.width = message.soundElem.duration/60*(MAXWIDTH/2) + self.avatarImg.height*2;
        _longLabel.text = [NSString stringWithFormat:@"%d", message.soundElem.duration];
        if (message.isSelf) {
            _voiceStr = message.soundElem.path;
            self.avatarImg.x = kScreenWidth - KScaleWidth(10.0) - self.avatarImg.width;
            [self.avatarImg sd_setImageWithURL:[NSURL URLWithString:senderAvatarImg] placeholderImage:ImageNamed(@"mine_avatar_icon")];
            
            _voiceBtn.x = kScreenWidth - self.avatarImg.width - KScaleWidth(10.0)*2 - _voiceBtn.width;
            _longLabel.x = _voiceBtn.width - _longLabel.width;
            _msgVoiceIcon.x = _longLabel.x - _msgVoiceIcon.width;
        }
        else {
            XMWeakSelf;
            [message.soundElem getUrl:^(NSString * _Nullable url) {
                weakSelf.voiceStr = url;
            }];
            self.avatarImg.x = KScaleWidth(10.0);
            self.voiceBtn.x = self.avatarImg.maxX + KScaleWidth(10.0);
            [self.avatarImg sd_setImageWithURL:[NSURL URLWithString:receiverAvatarImg] placeholderImage:ImageNamed(@"mine_avatar_icon")];
            _longLabel.x = 0;
            _msgVoiceIcon.x = _longLabel.maxX;
        }
    }
}

- (void)voicePlayAction:(UIButton *)button {
    if (_audioPlayer) {
        if ([_audioPlayer isPlaying]) {
            [_audioPlayer stop];
            _audioPlayer = nil;
        }
    }
    NSURL *url;
    if ([_voiceStr containsString:@"http"]) {
        url = [NSURL URLWithString:_voiceStr];
    }
    else {
        url = [NSURL fileURLWithPath:_voiceStr];
    }
    NSError *error = nil;
    _audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    if (!error) {
        if ([_audioPlayer prepareToPlay]) {
            [_audioPlayer play];
        }
    }
}

#pragma mark - Init
//- (UIImageView *)avatarImg {
//    if (!_avatarImg) {
//        _avatarImg = [[UIImageView alloc] initWithFrame:CGRectMake(KScaleWidth(10.0), KScaleWidth(8.0), KScaleWidth(44.0), KScaleWidth(44.0))];
//        [_avatarImg setCornerRadius:KScaleWidth(10.0)];
//    }
//    return _avatarImg;
//}

- (UIButton *)voiceBtn {
    if (!_voiceBtn) {
        _voiceBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _voiceBtn.frame = CGRectMake(self.avatarImg.maxX + KScaleWidth(10.0), self.avatarImg.y, kScreenWidth - self.avatarImg.maxX - KScaleWidth(10.0) - self.avatarImg.width - KScaleWidth(10.0), self.avatarImg.height);
        _voiceBtn.backgroundColor = kColorHEX(0xffffff);
        [_voiceBtn setCornerRadius:KScaleWidth(10.0)];
        [_voiceBtn addTarget:self action:@selector(voicePlayAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _voiceBtn;
}

- (UILabel *)longLabel {
    if (!_longLabel) {
        _longLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, KScaleWidth(40.0), self.voiceBtn.height) font:KBoldFontSize(14.0) textColor:kColorHEX(0x333333) textAlignment:NSTextAlignmentCenter];
    }
    return _longLabel;
}

- (UIImageView *)msgVoiceIcon {
    if (!_msgVoiceIcon) {
        _msgVoiceIcon = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, KScaleWidth(18.0), self.avatarImg.height)];
        _msgVoiceIcon.contentMode = UIViewContentModeScaleAspectFit;
        _msgVoiceIcon.image = ImageNamed(@"chat_msg_voice_icon");
    }
    return _msgVoiceIcon;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
